package com.egao.common.core.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;

import java.util.concurrent.CompletableFuture;

/**
 * 阿里云短信
 * @author lym
 * @date 2022/10/9 15:06
 */
@Profile("prod")
public class SendSmsUtil {

    /**
     * 发送短信
     * @param phoneNumbers 手机号 可以多个英文逗号隔开
     * @param templateCode 模板编码
     * @throws Exception
     */
    @Async
    public void send(String phoneNumbers, String templateCode) {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tAkqjcWFKgG7gxfG6Vx")
                .accessKeySecret("nOGn0WlIE2ydtO2nshOZfevanxKuNQ")
                .build()
        );

        AsyncClient client = AsyncClient.builder()
                .region("cn-shanghai")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                        .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phoneNumbers)
                .signName("中南大就业指导服务中心")
                .templateCode(templateCode)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        //SendSmsResponse res = response.get();
        //String result = new Gson().toJson(res);

        client.close();
    }


}
